package com.dhcc.bpm.modules.orga.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.common.enums.ConfigType;
import com.dhcc.bpm.common.redisMessage.RedisMessageSender;
import com.dhcc.bpm.modules.orga.entity.App;
import com.dhcc.bpm.modules.orga.entity.ProduceApply;
import com.dhcc.bpm.modules.orga.service.IAppApiService;
import com.dhcc.bpm.modules.orga.service.IAppService;
import com.dhcc.bpm.modules.orga.service.IProduceApplyService;
import com.dhcc.bpm.modules.orga.vo.AppApiVO;
import com.dhcc.bpm.modules.orga.vo.AppVO;
import com.dhcc.bpm.modules.orga.vo.ProduceApplyDetailVO;
import com.dhcc.bpm.modules.orga.vo.ProduceApplyVO;
import com.dhcc.bpm.modules.workflow.service.IWorkFlowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName ProduceApplyController
 * @Description 测试申请审批表 控制器
 * @Author daina
 * @Date 2020/2/24 19:25
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("bpm-orga/produceapply")
@Api(value = "测试申请审批表", tags = "测试申请审批表接口")
public class ProduceApplyController extends BladeController {
	private final static Logger log = LoggerFactory.getLogger(ProduceApplyController.class);
	@Autowired
	private RedisMessageSender redisMessageSender;
	private IProduceApplyService produceApplyService;

	private IAppService appService;

	private IAppApiService appApiService;
	@Autowired
	private IWorkFlowService iWorkFlowService;
	/**
	 * @Description 详情
	 * @Author daina
	 * @Date 10:44 2020/2/25
	 * @param produceApply
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.orga.vo.ProduceApplyDetailVO>
	 **/
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入produceApply")
	@ApiLog("测试申请审批表 详情")
	@Log(title = "测试申请审批表 详情", businessType = BusinessType.SELECT)
	public R<ProduceApplyDetailVO> detail(ProduceApplyVO produceApply) {
//		ProduceApply produceApplydetail = produceApplyService.getOne(Condition.getQueryWrapper(produceApply));
		//ProduceApply produceApplydetail = produceApplyService.getById(produceApply.getId());
		ProduceApplyVO produceApplydetail = produceApplyService.selectProApplyById(produceApply.getId());
		App app = new App();
		app.setId(produceApplydetail.getAppId());
		App appDetail = appService.getOne(Condition.getQueryWrapper(app));
		List<AppApiVO> appApiList = appApiService.findApiList(appDetail, "1");
		ProduceApplyDetailVO produceApplyDetailVO = new ProduceApplyDetailVO();
		produceApplyDetailVO.setProduceApply(produceApplydetail);
		produceApplyDetailVO.setApp(appDetail);
		produceApplyDetailVO.setApiList(appApiList);
		return R.data(produceApplyDetailVO);
	}

	@GetMapping("/proDetail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入produceApply")
	@ApiLog("测试申请审批表 详情")
	@Log(title = "测试申请审批表 详情", businessType = BusinessType.SELECT)
	public R proDetail(ProduceApply produceApply) {
		ProduceApply produceApplydetail = produceApplyService.getById(produceApply.getId());
		return R.data(produceApplydetail);
	}

	/**
	 * @Description 测试申请审批表
	 * @Author daina
	 * @Date 10:44 2020/2/25
	 * @param produceApply
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.orga.entity.ProduceApply>>
	 **/
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入produceApply")
	@ApiLog("测试申请审批表 分页")
	@Log(title = "测试申请审批表 分页", businessType = BusinessType.SELECT)
	public R<IPage<ProduceApply>> list(ProduceApply produceApply, Query query) {
		IPage<ProduceApply> pages = produceApplyService.page(Condition.getPage(query), Condition.getQueryWrapper(produceApply));
		return R.data(pages);
	}

	/**
	 * @Description 测试申请审批表
	 * @Author daina
	 * @Date 10:44 2020/2/25
	 * @param produceApply
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.orga.vo.ProduceApplyVO>>
	 **/
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入produceApply")
	@ApiLog("测试申请审批表 分页")
	@Log(title = "测试申请审批表 分页", businessType = BusinessType.SELECT)
	public R<IPage<ProduceApplyVO>> page(ProduceApplyVO produceApply, Query query) {
		IPage<ProduceApplyVO> pages = produceApplyService.selectProduceApplyPage(Condition.getPage(query), produceApply);
		pages.getRecords().forEach(item ->{
			item.setChangeNum(produceApplyService.selectChangeNum(item.getId().toString()));
		});
		return R.data(pages);
	}

	/**
	 * @Description 测试申请审批表变更列表
	 * @Author daina
	 * @Date 10:44 2020/2/25
	 * @param produceApply
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.orga.vo.ProduceApplyVO>>
	 **/
	@GetMapping("/changePage")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入produceApply")
	@ApiLog("测试申请审批表变更列表")
	@Log(title = "测试申请审批表变更列表", businessType = BusinessType.SELECT)
	public R<IPage<ProduceApplyVO>> changePage(ProduceApplyVO produceApply, Query query) {
		IPage<ProduceApplyVO> pages = produceApplyService.selectProduceApplyChangePage(Condition.getPage(query), produceApply);
		return R.data(pages);
	}

	/**
	 * @Description 新增 测试申请审批表
	 * @Author daina
	 * @Date 10:44 2020/2/25
	 * @param produceApply
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiLog("测试申请审批表 新增")
	@Log(title = "测试申请审批表 新增", businessType = BusinessType.INSERT)
	@ApiOperation(value = "新增", notes = "传入produceApply")
	public R save(@Valid @RequestBody ProduceApply produceApply) {
		return R.status(produceApplyService.save(produceApply));
	}

	/**
	 * @Description 修改 测试申请审批表
	 * @Author daina
	 * @Date 10:44 2020/2/25
	 * @param produceApply
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入produceApply")
	@ApiLog("测试申请审批表 修改")
	@Log(title = "测试申请审批表 修改", businessType = BusinessType.UPDATE)
	public R update(@Valid @RequestBody ProduceApply produceApply) {
		return R.status(produceApplyService.updateById(produceApply));
	}

	/**
	 * @Description 新增或修改 测试申请审批表
	 * @Author daina
	 * @Date 10:45 2020/2/25
	 * @param produceApply
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入produceApply")
	@ApiLog("测试申请审批表 新增或修改")
	@Log(title = "测试申请审批表 新增或修改", businessType = BusinessType.INSERT)
	@Transactional(rollbackFor = Exception.class)
	public R submit(@Valid @RequestBody ProduceApplyVO produceApply) {
		List<AppVO> list = appService.findByAppId(produceApply.getPayAppId());
		if(list.size() > 0){
			return R.fail(223, "该渠道号已存在，请填写新的渠道号！");
		}
        produceApplyService.saveOrUpdate(produceApply);
        ProduceApply byId = produceApplyService.getById(produceApply.getId());
        App app = new App();
        //修改渠道号
        app.setId(byId.getAppId());
        app.setAppId(produceApply.getPayAppId());
        boolean bool = appService.updateById(app);
        if(bool){
			String message = redisMessageSender.format(ConfigType.APPSECRET.name(), "刷新应用/渠道公私钥信息");
			redisMessageSender.sendMessageToGateway(null, message);
		}
        return R.status(bool);
	}

	
	/**
	 * @Description 删除 测试申请审批表
	 * @Author daina
	 * @Date 10:45 2020/2/25
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "测试申请审批表 删除", notes = "传入ids")
	@ApiLog("测试申请审批表 删除")
	@Log(title = "测试申请审批表 删除", businessType = BusinessType.DELETE)
	@Transactional(rollbackFor = Exception.class)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		String[] arrayIds=ids.split(",");
		for (String id:  arrayIds) {
			iWorkFlowService.deleteRelated("m",id);
		}
		return R.status(produceApplyService.deleteLogic(Func.toLongList(ids)));
	}

	
}
